package com.ruoyi.user.controller;

import java.io.IOException;
import java.util.List;

import com.ruoyi.coupon.domain.dto.RechargeCouponDTO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.user.domain.User;
import com.ruoyi.user.service.IUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 用户管理Controller
 * 
 * @author qingshi
 * @date 2021-06-04
 */
@RestController
@RequestMapping("/user/userManage")
public class UserController extends BaseController
{
    @Autowired
    private IUserService userService;

    /**
     * 查询用户管理列表
     */
    @PreAuthorize("@ss.hasPermi('user:userManage:list')")
    @GetMapping("/list")
    public TableDataInfo list(User user)
    {
        startPage();
        List<User> list = userService.selectUserList(user);
        return getDataTable(list);
    }

    /**
     * 导出用户管理列表
     */
    @PreAuthorize("@ss.hasPermi('user:userManage:export')")
    @Log(title = "用户管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(User user)
    {
        List<User> list = userService.selectUserList(user);
        ExcelUtil<User> util = new ExcelUtil<User>(User.class);
        return util.exportExcel(list, "用户管理数据");
    }

    /**
     * 获取用户管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('user:userManage:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(userService.selectUserById(id));
    }

    /**
     * 新增用户管理
     */
    @PreAuthorize("@ss.hasPermi('user:userManage:add')")
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody User user)
    {
        return toAjax(userService.insertUser(user));
    }

    /**
     * 修改用户管理
     */
    @PreAuthorize("@ss.hasPermi('user:userManage:edit')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody User user)
    {
        return toAjax(userService.updateUser(user));
    }

    /**
     * 删除用户管理
     */
    @PreAuthorize("@ss.hasPermi('user:userManage:remove')")
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userService.deleteUserByIds(ids));
    }

    /**
     * 给用户添加优惠券
     */
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @PostMapping("/recharge/{ids}/{amount}/{discount}")
    public AjaxResult recharge(@PathVariable Long[] ids, @PathVariable Integer amount, @PathVariable Integer discount){
        System.out.println(ids+" " + amount+ " " +discount);

        userService.recharge(ids,amount,discount);

        return AjaxResult.success("添加优惠券成功");

    }

    /**
     * 批量导入员工
     */
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @PostMapping("/import")
    public AjaxResult importStaff(MultipartFile staffFile) throws Exception {

        System.out.println("进入controller");
        System.out.println(staffFile.getSize());
        ExcelUtil<User> util = new ExcelUtil<User>(User.class);
        List<User> users = util.importExcel(staffFile.getInputStream());

        users.forEach(user -> {
            System.out.println(user.toString());
        });

        userService.importStaff(users);

        return AjaxResult.success("导入成功");
    }
}
